package cn.zyjblogs.server.role.controller;

import cn.zyjblogs.server.role.dto.RolePageDto;
import cn.zyjblogs.server.role.po.RoleDto;
import cn.zyjblogs.server.role.po.RolePo;
import cn.zyjblogs.server.role.service.RoleService;
import cn.zyjblogs.server.role.vo.RoleVo;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.common.utils.bean.BeanUtils;
import cn.zyjblogs.starter.web.apiversion.ApiVersion;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuyijun
 */
@Api(tags = {"角色管理"})
@RestController
@RequestMapping("/{v}/role")
@ResponseBody
@ApiVersion(1)
@RequiredArgsConstructor
public class RoleController {
    private final RoleService roleService;

    /**
     * 通过id查询用户信息
     *
     * @param id
     * @return cn.zyjblogs.rbac.server.user.po.UserPo
     * @author zhuyijun
     * @date 2022/9/19 上午12:06
     */
    @ApiOperation(value = "通过id查询角色", notes = "通过id查询角色")
    @ApiVersion(1)
    @GetMapping("/id")
    public ResponseObject<RoleVo> findById(@RequestParam String id) {
        RolePo byId = roleService.getById(id);
        return ResponseResult.success(BeanUtils.map(byId, RoleVo.class));
    }


    /**
     * @param rolePageDto
     * @return cn.zyjblogs.starter.common.entity.response.ResponseObject<com.baomidou.mybatisplus.extension.plugins.pagination.Page < cn.zyjblogs.rbac.server.user.po.UserPo>>
     * @author zhuyijun
     * @date 2022/9/19 上午1:14
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiVersion(1)
    @GetMapping("/page")
    public ResponseObject<List<RoleVo>> findPage(RolePageDto rolePageDto) {
        IPage<RoleVo> page = roleService.findPage(rolePageDto);
        return ResponseResult.success(page.getTotal(), page.getRecords());
    }

    /**
     * @param
     * @return cn.zyjblogs.starter.common.entity.response.ResponseObject<java.util.List < cn.zyjblogs.server.role.vo.RoleVo>>
     * @author zhuyijun
     * @date 2022/9/22 下午11:33
     */
    @ApiOperation(value = "查询当前租户下所有角色", notes = "查询所有角色")
    @ApiVersion(1)
    @GetMapping("/list")
    public ResponseObject<List<RoleVo>> findList() {
        return ResponseResult.success(roleService.findList(BaseContext.getTenantId()));
    }

    /**
     * 通过id集合查询
     *
     * @param ids
     * @return cn.zyjblogs.starter.common.entity.response.ResponseObject<com.baomidou.mybatisplus.extension.plugins.pagination.Page < cn.zyjblogs.rbac.server.user.po.UserPo>>
     * @author zhuyijun
     * @date 2022/9/19 上午1:14
     */
    @ApiOperation(value = "通过角色id查询", notes = "通过角色id查询")
    @ApiVersion(1)
    @PostMapping("/findByIds")
    public ResponseObject<List<RoleVo>> findByIds(List<String> ids) {
        return ResponseResult.success(roleService.findByIds(ids));
    }

    /**
     * 角色保存
     *
     * @param roleDto
     * @return cn.zyjblogs.starter.common.entity.response.ResponseObject<com.baomidou.mybatisplus.extension.plugins.pagination.Page < cn.zyjblogs.rbac.server.user.po.UserPo>>
     * @author zhuyijun
     * @date 2022/9/19 上午1:14
     */
    @ApiOperation(value = "通过角色id查询", notes = "通过角色id查询")
    @ApiVersion(1)
    @PostMapping("/save")
    public ResponseObject<Object> save(@Validated @RequestBody RoleDto roleDto) {
        roleService.save(roleDto);
        return ResponseResult.success();
    }

    /**
     * 角色更新
     *
     * @param roleDto
     * @return cn.zyjblogs.starter.common.entity.response.ResponseObject<com.baomidou.mybatisplus.extension.plugins.pagination.Page < cn.zyjblogs.rbac.server.user.po.UserPo>>
     * @author zhuyijun
     * @date 2022/9/19 上午1:14
     */
    @ApiOperation(value = "通过角色id查询", notes = "通过角色id查询")
    @ApiVersion(1)
    @PostMapping("/update")
    public ResponseObject<Object> update(@Validated @RequestBody RoleDto roleDto) {
        roleService.update(roleDto);
        return ResponseResult.success();
    }
}
